#!/bin/bash
# setup-web2py-uwsgi-cherokee-ubuntu.sh
# http://web2pyslices.com/main/slices/take_slice/110
/usr/sbin/locale-gen en_US.UTF-8
/usr/sbin/update-locale LANG=en_US.UTF-8

apt-get update
apt-get -y upgrade

apt-get -y install wget zip unzip tar
apt-get -y install build-essential
apt-get -y install python-dev
apt-get -y install python-software-properties
apt-get -y install libxml2-dev

# install web2py
cd /var
rm web2py_src.zip*
wget http://web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
chown -R www-data:www-data /var/web2py

# install uwsgi
cd /var
wget http://projects.unbit.it/downloads/uwsgi-0.9.6.5.tar.gz
tar -zxvf uwsgi-0.9.6.5.tar.gz
cd uwsgi-0.9.6.5
python uwsgiconfig.py --build
cp uwsgi /usr/local/bin/uwsgi
echo '
<uwsgi>
    <pythonpath>/var/web2py/</pythonpath>
    <app mountpoint="/">
    <script>wsgihandler</script>
    </app>
</uwsgi>
' > /var/web2py/config.xml

#install cherokee
add-apt-repository ppa:cherokee-webserver
apt-get update
apt-get -y install cherokee
apt-get -y install rrdtool
apt-get -y install libcherokee-mod-rrd
apt-get -y install libcherokee-mod-libssl

# self-signed certs
mkdir /etc/cherokee/ssl
cd /etc/cherokee/ssl
dd if=/dev/random of=rand.dat bs=1024 count=1
openssl genrsa -out cakey.key -rand rand.dat 2048
openssl req -new -key cakey.key -out cakey.csr
openssl x509 -req -days 1780 -set_serial 1 -in cakey.csr -signkey cakey.key -out cakey.pem
cat cakey.key cakey.pem > server.pem
rm -f rand.dat

# set password for web2py
cd /var/web2py
sudo -u www-data python -c "from gluon.widget import console; console();"
sudo -u www-data python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),443)"
